package com.fly.caihong.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fly.caihong.R;
import com.fly.caihong.base.BaseActivity;
import com.fly.caihong.model.VideoItem;
import com.fly.caihong.utils.DataHandler;
import com.fly.caihong.utils.GlideUtils;
import com.fly.caihong.utils.StatusBarUtils;

import java.io.File;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class VideoDetailActivity extends BaseActivity {

    JCVideoPlayerStandard jcVideoPlayerStandard;

    @Override
    protected int getLayout() {
        return R.layout.activity_video_detail;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                jcVideoPlayerStandard.bottomContainer.setVisibility(View.INVISIBLE);
                mHandler.sendEmptyMessageDelayed(0, 20);
            }
        }
    };

    public static void launchActivity(Context context, VideoItem videoItem) {
        Intent itt = new Intent(context, VideoDetailActivity.class);
        itt.putExtra("data", videoItem);
        context.startActivity(itt);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setTransparent(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setStatusBarStyle() {
        StatusBarUtils.assistNavigationBar(this, findViewById(R.id.rl_root));
    }

    @Override
    protected void initViews() {
        super.initViews();
        VideoItem videoItem = (VideoItem) getIntent().getSerializableExtra("data");
        jcVideoPlayerStandard = findViewById(R.id.jc_video_player);
        jcVideoPlayerStandard.setUp(DataHandler.EXTERNAL_STORAGE_PATH + File.separator + DataHandler.dataPaths.get("video/video") + File.separator + videoItem.getVideolink()
                , JCVideoPlayerStandard.SCREEN_WINDOW_FULLSCREEN, "");
        JCVideoPlayerStandard.clearSavedProgress(this, DataHandler.EXTERNAL_STORAGE_PATH + "/caihong/video/" + videoItem.getVideolink());
        jcVideoPlayerStandard.startVideo();
        jcVideoPlayerStandard.dismissBrightnessDialog();
        jcVideoPlayerStandard.dismissProgressDialog();
        jcVideoPlayerStandard.dismissVolumeDialog();
        TextView tvName = findViewById(R.id.tv_name);
        tvName.setText(videoItem.getNickName());
        ImageView ivProfile = findViewById(R.id.iv_profile);
        GlideUtils.loadCircleAvatar(this, ivProfile, videoItem.getPhoto());
        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText(videoItem.getTitle());
        TextView tvLike = findViewById(R.id.tv_like);
        tvLike.setText(videoItem.getLikeCount());
        TextView tvReply = findViewById(R.id.tv_reply);
        tvReply.setText(videoItem.getReplyCount());
        jcVideoPlayerStandard.bottomContainer.setVisibility(View.GONE);
        mHandler.sendEmptyMessage(0);
    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeMessages(0);
    }

}
